package com.example.it.studymenu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SubMenuActivity extends AppCompatActivity {

    private Map<Integer, List<String>> _dataSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_menu);
    }

    /*
    * 初始化一些数据源
    * */
    void initData() {
        _dataSource = new HashMap<Integer, List<String>>();
        int[] ints = {1, 2, 3};
        for (int item : ints) {
            List<String> list = new ArrayList<String>();
            for (int i = 1; i <= 3; i++) {
                list.add("有关部门" + i);
            }
            this._dataSource.put(item, list);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //initData();
        //
        //for (Map.Entry<Integer, List<String>> item : _dataSource.entrySet()) {
        //    String menuName = "";
        //    switch (item.getKey()) {
        //        case 1:
        //            menuName = "有关部门";
        //            break;
        //        case 2:
        //            menuName = "负责部门";
        //            break;
        //        case 3:
        //            menuName = "相关部门";
        //            break;
        //    }
        //    SubMenu subMenu = menu.addSubMenu(Menu.NONE, item.getKey(), Menu.NONE, menuName);
        //    List<String> list = item.getValue();
        //    for (int i = 0; i < list.size(); i++) {
        //        subMenu.add(Menu.NONE, i, Menu.NONE, list.get(i));
        //    }
        //}

        MenuInflater mInflater = new MenuInflater(this);
        mInflater.inflate(R.menu.submenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //获取item的Title
        String title = (String) item.getTitle();
        Toast.makeText(this, title, Toast.LENGTH_SHORT).show();
        return super.onOptionsItemSelected(item);
    }
}
